package com.itheima.edu.info.manager.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public final class DateUtil {
    private DateUtil() {
    }

    public static int getAgeByBirthday(String birthday) {

        try {
            // 使用指定的日期模板获取一个SimpleDateFormat对象
            SimpleDateFormat pattern = new SimpleDateFormat("yyyy-MM-dd");

            //将pattern转成Date对象
            Date birDate = pattern.parse(birthday);

            //获取生日当天的毫秒值
            long birTime = birDate.getTime();

            //获取现在的时间
            Date now = new Date();
            long nowTime = now.getTime();

            //获取差值
            long ageTime = nowTime - birTime;

            //获取年龄
            int age = (int) (ageTime / (1000L * 60 * 60 * 24 * 365));
            return age;

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public static int getAgeByBirthday(String birthday,int abc){
        try {
            //1. 将birthday解析成一个LocalDateTime对象
            //birthday += " 00:00:00";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate birthTime = LocalDate.parse(birthday, formatter);

            //2. 拿到当前日期
            LocalDate now = LocalDate.now();

            //3. 计算两个日期的差值
            Period period = Period.between(birthTime, now);

            return period.getYears();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
